package cmd

import (
	"bytes"
	"fmt"
	"gitee.com/lllgo/opsctl/pkg/model"
	"github.com/spf13/cobra"
)

func logCmd() *cobra.Command {
	var recursive bool
	var tmp = &cobra.Command{
		Use:   "log",
		Short: "查看操作记录，显示id和命令参数",
		Run: func(cmd *cobra.Command, args []string) {
			logs := model.GetLog(args...)
			logShowName := "Command\t"

			if recursive {
				logShowName += "ExecuteTime\t"
			}

			sb := bytes.NewBufferString(logShowName + "\n")
			for _, log := range logs {
				sb.WriteString(log.Cmd + "\t")
				if recursive {
					sb.WriteString(log.CreatedAt)
				}
				sb.WriteString("\n")
			}

			fmt.Println(sb.String())
		},
	}
	tmp.Flags().BoolVarP(&recursive, "recursive", "r", false, "打印全部日志信息，默认只显示日志id和命令")
	return tmp
}
